package vcproj2cmake;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;

import vcproj2cmake.reader.SolutionReader;
import vcproj2cmake.writer.SolutionWriter;

import jtools.files.filesystem.FileDialog;

public class GUI extends JFrame {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JFrame jf = new JFrame("msvc2cmake");
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		GridBagLayout layout = new GridBagLayout();
		GridBagConstraints gc = new GridBagConstraints();
		
		JButton browse_sln = new JButton("Browse Solution");
		JButton browse_wor = new JButton("Browse Workspace");
		final JComboBox combo = new JComboBox();
		
		final JTextField tf_sln = new JTextField("...");
		final JTextField tf_wor = new JTextField("...");
		JButton create_button = new JButton("Create");
		
		
		browse_sln.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				String[] pref = {".sln"};
				File file = FileDialog.openDialog(null, pref, "Visual Studio Solution", false);
				if (file.exists()){
					tf_sln.setText(file.getAbsolutePath());
					try{
						SolutionReader sr = new SolutionReader();
						Solution solution = sr.readSolution(file, file.getParentFile());
						if (solution == null){
							System.err.println("Could not read solution file or projects");
							System.exit(1);
						}
						HashSet<String> config = solution.getConfigurations();
						combo.removeAllItems();
						Iterator<String> iter = config.iterator();
						while(iter.hasNext()){
							combo.addItem(iter.next());
						}
					}catch(Exception exp){
						exp.printStackTrace();
					}	
				}
			}
		});
		browse_wor.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				String[] pref = {""};
				File file = FileDialog.openDialog(null, pref, "Workspace", true);
				if (file.exists() && file.isDirectory())
					tf_wor.setText(file.getAbsolutePath());
			}
		});
		
		
		create_button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				File f_sln = new File(tf_sln.getText());
				File f_wor = new File(tf_wor.getText());
				if (!(f_sln.exists() && f_wor.exists())){
					System.err.println("Could not load files: "+f_sln+" and "+f_wor);
					return ;
				}
				SolutionReader sr = new SolutionReader();
				try{
					Solution solution = sr.readSolution(f_sln, f_wor);
					if (solution == null){
						System.err.println("Could not read solution file or projects");
						System.exit(1);
					}
					
					SolutionWriter sw = new SolutionWriter();
					sw.writeSolutionToCMakeLists(solution, (String)combo.getSelectedItem());
					System.out.println("Export succssesfull");
				}catch(Exception exp){
					exp.printStackTrace();
				}
			}
		});
		
		
		
		gc.gridx = 0; gc.gridy = 0;
		gc.fill = GridBagConstraints.BOTH;
		
		jf.setLayout(layout);
		jf.add(tf_sln, gc);
		gc.gridy = 1;
		jf.add(browse_sln, gc);
		
		gc.gridy = 0; gc.gridx = 1;
		jf.add(tf_wor, gc);
		gc.gridy = 1;
		jf.add(browse_wor, gc);
		
		gc.gridx = 0;
		gc.gridy = 2;
		gc.gridwidth = 2;
		jf.add(combo, gc);
		
		gc.gridx = 0;
		gc.gridy = 3;
		gc.gridwidth = 2;
		jf.add(create_button, gc);
		
		jf.setVisible(true);
		jf.pack();
	}

}
